﻿#pragma once
#include "OmPublic\OmMutex.h"

struct UBox
{
	wstring strGpsInfo = L"FA FA F4 00 00 09 8C 73 B9 6F 00 22 09 36 09 11 07 0A 01 57 C9 F6 06 CA 8D F0 00 00 53 00 00 05 32 07 00 01 00 91 0D 05 80 1E 03 1B 6D 79 6D FB";
	wstring strCanStaticData = L"FA FA 47 01 00 09 8C 73 B9 6F 00 25 06 24 2D 11 06 0F 00 7F 0C 30 00 03 01 00 09 00 8F 03 3E 0F 09 09 00 19 00 23 03 00 46 00 01 3D FA 00 00 1F E6 E1 FB";
	wstring strCarFault = L"FA FA 18 02 00 09 8C 73 B9 6F 00 3E 09 08 31 11 07 0A 0B 7F 00 7F 01 02 7F 00 7F 43 04 7F 00 7F 85 06 7F 00 7F C7 08 7F 00 7F 09 0A 7F 00 7F 0B 0C 7F 00 7F 0D 0E 7F 00 7F 0F 10 7F 00 7F 11 12 7F 00 7F 13 14 7F 00 7F 15 16 34 FB";
	wstring strVersionInfo = L"FA FA 45 03 00 09 8C 73 B9 6F 00 30 54 49 44 41 47 31 30 33 56 39 30 39 32 30 31 37 2D 30 34 2D 31 37 47 49 44 34 2E 30 20 20 46 33 31 33 33 34 63 20 42 54 4C 44 56 34 30 30 00 03 77 FB";
	wstring strTerminalWarning = L"FA FA 1E 04 00 09 8C 73 B9 6F 00 23 0B 0C 2C 08 06 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 04 00 00 00 7C 10 00 00 00 02 00 00 80 18 CA FB";
	wstring strTerminalHeartbeat = L"FA FA 47 05 00 09 8C 73 B9 6F 00 00 62 FB";
	wstring strActionData = L""; //驾驶行为数据
	wstring strSimInfo = L"FA FA 42 07 00 09 8C 73 B9 6F 00 37 34 36 30 30 30 30 36 39 30 33 32 36 32 32 38 38 39 38 36 30 30 35 33 31 39 31 34 30 38 30 31 34 38 31 36 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 66 FB";
	wstring strGpsLbsInfo = L""; //GPS+LBS
	wstring strLbsInfo = L"FA FA 44 09 00 09 8C 73 B9 6F 00 12 01 0A 05 11 07 0B 00 00 00 01 01 CC 00 24 7C 00 C4 12 2E FB";
	wstring strCarInfo = L"FA FA 0F 0A 00 09 8C 73 B9 6F 00 29 01 30 00 00 00 04 4C 53 56 43 41 30 30 30 30 43 30 30 30 38 30 30 31 0E 01 30 30 33 34 34 36 37 36 34 30 30 32 30 31 36 39 0B FB";
	wstring strHurryDecelerate = L""; //急减速
	wstring strHurryAcceleration = L""; //急加速
	wstring strHurrySwerves = L""; //急转弯
	wstring strOverSpeed = L""; //超速
	wstring strHurryBrakes = L"";//急刹车

	wstring strLog = L"";//日志
	wstring strAlarmEvent = L"FA FA 7C 97 00 09 8C 73 B9 6F 00 25 09 21 31 11 07 0A 01 57 CA 0E 06 CA 8D 20 00 00 3B 00 00 01 22 06 00 00 00 91 00 02 00 00 01 FF 78 00 00 23 6C 5C FB"; //报警事件
	wstring strExtendEvent = L"FA FA E6 9E 00 09 8C 73 B9 6F 00 32 0B 0B 0D 08 06 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 7A 01 00 01 00 01 00 11 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03 FB";//扩展事件
	wstring strBigGpsInfo = L"FA FA 0A A0 00 09 8C 73 B9 6F 01 5F 0A 22 09 14 18 11 07 0A 01 57 CB FA 06 CA 8F C8 00 00 82 00 00 0B 4A 08 00 01 00 7A 0D 05 60 1D 22 EA E8 89 22 09 14 1A 11 07 0A 01 57 CB FA 06 CA 8F C8 00 00 82 00 00 0B 4A 08 00 01 00 7A 00 00 60 1D 02 68 E0 89 22 09 14 38 11 07 0A 01 57 CB FA 06 CA 8F C8 00 00 82 00 00 0B 4A 08 00 01 00 7A 0D 05 60 1D 22 FA AA 89 22 09 17 33 11 07 0A 01 57 CB 1C 06 CA 8E 48 00 00 5A 00 00 0B 4A 04 00 01 00 8A 0D 05 60 1A 22 C9 E6 69 22 09 18 33 11 07 0A 01 57 C8 E2 06 CA 8A D8 00 00 0E 00 00 09 42 05 00 01 00 8D 0D 05 60 1A 23 0B 2B 59 22 09 19 15 11 07 0A 01 57 C9 6C 06 CA 8B B8 00 00 0C 00 00 09 42 06 00 01 00 8D 00 00 60 1A 22 79 80 69 22 09 19 33 11 07 0A 01 57 CA 64 06 CA 8D 80 00 00 42 00 00 01 A4 05 00 01 00 90 00 00 60 1A 22 DA A9 59 22 09 1A 15 11 07 0A 01 57 CA C6 06 CA 8E 48 00 00 59 00 00 02 12 06 00 01 00 90 00 00 60 1A 02 48 5F 69 22 09 1A 34 11 07 0A 01 57 CA E6 06 CA 8E 88 00 00 64 00 00 02 30 06 00 01 00 90 00 00 60 1A 22 A9 63 69 22 09 1B 16 11 07 0A 01 57 CA EE 06 CA 8E 88 00 00 68 00 00 02 30 05 00 01 00 90 00 00 60 00 22 89 24 59 79 FB"; //上报大GPS信息
	wstring strBigCanStaticData = L""; //上报大CAN静态数据
	wstring strHurryDecelerateScene = L"FA FA 0A A8 00 00 03 E7 FC 88 01 E9 03 14 01 11 03 0E 11 AA 01 FB E5 5A 07 1F EA 98 00 50 00 B4 03 2A 01 FB E5 5C 07 1F EA 9A 00 50 00 B4 03 2A 01 FB E5 5E 07 1F EA 9C 00 50 00 B4 03 2A 01 FB E5 60 07 1F EA 9E 00 50 00 B4 03 2A 01 FB E5 62 07 1F EA A0 00 50 00 B4 03 2A 01 FB E5 64 07 1F EA A2 00 50 00 B4 03 2A 01 FB E5 66 07 1F EA A4 00 50 00 B4 03 2A 01 FB E5 68 07 1F EA A6 00 50 00 B4 03 2A 01 FB E5 6A 07 1F EA A8 00 50 00 B4 03 2A 01 FB E5 6C 07 1F EA AA 00 50 00 B4 03 2A 01 FB E5 6E 07 1F EA AC 00 50 00 B4 03 2A 01 FB E5 70 07 1F EA AE 00 50 00 B4 03 2A 01 FB E5 72 07 1F EA B0 00 50 00 B4 03 2A 01 FB E5 74 07 1F EA B2 00 50 00 B4 03 2A 01 FB E5 76 07 1F EA B4 00 50 00 B4 03 2A 01 FB E5 78 07 1F EA B6 00 50 00 B4 03 2A 01 FB E5 7A 07 1F EA B8 00 50 00 B4 03 2A 01 FB E5 7C 07 1F EA BA 00 50 00 B4 03 2A 01 FB E5 7E 07 1F EA BC 00 50 00 B4 03 2A 01 FB E5 80 07 1F EA BE 00 50 00 B4 03 2A 01 FB E5 82 07 1F EA C0 00 50 00 B4 03 2A 01 FB E5 84 07 1F EA C2 00 50 00 B4 03 2A 01 FB E5 86 07 1F EA C4 00 50 00 B4 03 2A 01 FB E5 88 07 1F EA C6 00 50 00 B4 03 2A 01 FB E5 8A 07 1F EA C8 00 50 00 B4 03 2A 01 FB E5 8C 07 1F EA CA 00 50 00 B4 03 2A 01 FB E5 8E 07 1F EA CC 00 50 00 B4 03 2A 01 FB E5 90 07 1F EA CE 00 50 00 B4 03 2A 01 FB E5 92 07 1F EA D0 00 50 00 B4 03 2A 01 FB E5 94 07 1F EA D2 00 50 00 B4 03 2A 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 0A AD FB";//急减速事件场景
	wstring strHurryAccelerationScene = L"FA FA 0B A9 00 00 03 E7 FC 88 01 E9 03 14 06 11 03 0E 11 AA 01 FB E3 2C 07 1F EC 68 00 50 00 B4 03 2A 01 FB E3 2E 07 1F EC 6A 00 50 00 B4 03 2A 01 FB E3 30 07 1F EC 6C 00 50 00 B4 03 2A 01 FB E3 32 07 1F EC 6E 00 50 00 B4 03 2A 01 FB E3 34 07 1F EC 70 00 50 00 B4 03 2A 01 FB E3 36 07 1F EC 72 00 50 00 B4 03 2A 01 FB E3 38 07 1F EC 74 00 50 00 B4 03 2A 01 FB E3 3A 07 1F EC 76 00 50 00 B4 03 2A 01 FB E3 3C 07 1F EC 78 00 50 00 B4 03 2A 01 FB E3 3E 07 1F EC 7A 00 50 00 B4 03 2A 01 FB E3 40 07 1F EC 7C 00 50 00 B4 03 2A 01 FB E3 42 07 1F EC 7E 00 50 00 B4 03 2A 01 FB E3 44 07 1F EC 80 00 50 00 B4 03 2A 01 FB E3 46 07 1F EC 82 00 50 00 B4 03 2A 01 FB E3 48 07 1F EC 84 00 50 00 B4 03 2A 01 FB E3 4A 07 1F EC 86 00 50 00 B4 03 2A 01 FB E3 4C 07 1F EC 88 00 50 00 B4 03 2A 01 FB E3 4E 07 1F EC 8A 00 50 00 B4 03 2A 01 FB E3 50 07 1F EC 8C 00 50 00 B4 03 2A 01 FB E3 52 07 1F EC 8E 00 50 00 B4 03 2A 01 FB E3 54 07 1F EC 90 00 50 00 B4 03 2A 01 FB E3 56 07 1F EC 92 00 50 00 B4 03 2A 01 FB E3 58 07 1F EC 94 00 50 00 B4 03 2A 01 FB E3 5A 07 1F EC 96 00 50 00 B4 03 2A 01 FB E3 5C 07 1F EC 98 00 50 00 B4 03 2A 01 FB E3 5E 07 1F EC 9A 00 50 00 B4 03 2A 01 FB E3 60 07 1F EC 9C 00 50 00 B4 03 2A 01 FB E3 62 07 1F EC 9E 00 50 00 B4 03 2A 01 FB E3 64 07 1F EC A0 00 50 00 B4 03 2A 01 FB E3 66 07 1F EC A2 00 50 00 B4 03 2A 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 0A 66 FB";//急加速事件场景
	wstring strHurrySwervesScene = L"FA FA 19 AA 00 09 8C 73 B9 6F 01 89 01 37 0D 11 07 1A 11 A5 00 00 00 00 00 00 00 00 04 D8 00 00 10 84 00 00 00 00 00 00 00 00 04 D8 00 00 10 86 00 00 00 00 00 00 00 00 04 D8 00 00 10 84 00 00 00 00 00 00 00 00 04 D8 00 00 10 84 00 00 00 00 00 00 00 00 04 E2 00 00 10 82 00 00 00 00 00 00 00 00 04 E2 00 00 10 82 00 00 00 00 00 00 00 00 04 D8 00 00 10 82 00 00 00 00 00 00 00 00 04 D8 00 00 10 84 00 00 00 00 00 00 00 00 04 D8 00 00 10 84 00 00 00 00 00 00 00 00 04 D8 00 00 10 84 00 00 00 00 00 00 00 00 04 D8 00 00 10 84 00 00 00 00 00 00 00 00 04 D8 00 00 10 82 00 00 00 00 00 00 00 00 04 D8 00 00 10 82 00 00 00 00 00 00 00 00 04 D8 00 00 10 86 00 00 00 00 00 00 00 00 04 D8 0A 28 10 86 00 00 00 00 00 00 00 00 04 D8 0A 28 10 84 00 00 00 00 00 00 00 00 04 D8 0A 28 10 84 00 00 00 00 00 00 00 00 04 D8 0A 28 10 86 00 00 00 00 00 00 00 00 04 D8 0A 28 10 86 00 00 00 00 00 00 00 00 04 D8 0A 28 10 86 00 00 00 00 00 00 00 00 04 D8 0A 28 10 86 00 00 00 00 00 00 00 00 04 D8 0A 28 10 86 00 00 00 00 00 00 00 00 04 D8 09 7E 10 86 00 00 00 00 00 00 00 00 04 D8 09 7E 10 86 00 00 00 00 00 00 00 00 04 D8 09 7E 10 86 00 00 00 03 00 00 00 03 00 03 00 00 00 01 00 01 00 00 00 06 00 01 00 01 00 00 00 03 00 01 04 03 E8 04 D8 97 FB"; //急转弯事件场景
	wstring strImpactEventScene = L"FA FA 0E AB 00 00 03 E7 FC 88 01 E9 03 14 15 11 03 0E 11 AA 01 FB DC A2 07 1F F1 E0 00 50 00 B4 03 2A 01 FB DC A4 07 1F F1 E2 00 50 00 B4 03 2A 01 FB DC A6 07 1F F1 E4 00 50 00 B4 03 2A 01 FB DC A8 07 1F F1 E6 00 50 00 B4 03 2A 01 FB DC AA 07 1F F1 E8 00 50 00 B4 03 2A 01 FB DC AC 07 1F F1 EA 00 50 00 B4 03 2A 01 FB DC AE 07 1F F1 EC 00 50 00 B4 03 2A 01 FB DC B0 07 1F F1 EE 00 50 00 B4 03 2A 01 FB DC B2 07 1F F1 F0 00 50 00 B4 03 2A 01 FB DC B4 07 1F F1 F2 00 50 00 B4 03 2A 01 FB DC B6 07 1F F1 F4 00 50 00 B4 03 2A 01 FB DC B8 07 1F F1 F6 00 50 00 B4 03 2A 01 FB DC BA 07 1F F1 F8 00 50 00 B4 03 2A 01 FB DC BC 07 1F F1 FA 00 50 00 B4 03 2A 01 FB DC BE 07 1F F1 FC 00 50 00 B4 03 2A 01 FB DC C0 07 1F F1 FE 00 50 00 B4 03 2A 01 FB DC C2 07 1F F2 00 00 50 00 B4 03 2A 01 FB DC C4 07 1F F2 02 00 50 00 B4 03 2A 01 FB DC C6 07 1F F2 04 00 50 00 B4 03 2A 01 FB DC C8 07 1F F2 06 00 50 00 B4 03 2A 01 FB DC CA 07 1F F2 08 00 50 00 B4 03 2A 01 FB DC CC 07 1F F2 0A 00 50 00 B4 03 2A 01 FB DC CE 07 1F F2 0C 00 50 00 B4 03 2A 01 FB DC D0 07 1F F2 0E 00 50 00 B4 03 2A 01 FB DC D2 07 1F F2 10 00 50 00 B4 03 2A 01 FB DC D4 07 1F F2 12 00 50 00 B4 03 2A 01 FB DC D6 07 1F F2 14 00 50 00 B4 03 2A 01 FB DC D8 07 1F F2 16 00 50 00 B4 03 2A 01 FB DC DA 07 1F F2 18 00 50 00 B4 03 2A 01 FB DC DC 07 1F F2 1A 00 50 00 B4 03 2A 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 0A 0E FB"; //碰撞事件场景
	wstring strHurryBrakesEventScene = L"FA FA 0C AC 00 00 03 E7 FC 88 01 E9 03 14 0B 11 03 0E 11 AA 01 FB E0 FC 07 1F EE 40 00 50 00 B4 03 2A 01 FB E0 FE 07 1F EE 42 00 50 00 B4 03 2A 01 FB E1 00 07 1F EE 44 00 50 00 B4 03 2A 01 FB E1 02 07 1F EE 46 00 50 00 B4 03 2A 01 FB E1 04 07 1F EE 48 00 50 00 B4 03 2A 01 FB E1 06 07 1F EE 4A 00 50 00 B4 03 2A 01 FB E1 08 07 1F EE 4C 00 50 00 B4 03 2A 01 FB E1 0A 07 1F EE 4E 00 50 00 B4 03 2A 01 FB E1 0C 07 1F EE 50 00 50 00 B4 03 2A 01 FB E1 0E 07 1F EE 52 00 50 00 B4 03 2A 01 FB E1 10 07 1F EE 54 00 50 00 B4 03 2A 01 FB E1 12 07 1F EE 56 00 50 00 B4 03 2A 01 FB E1 14 07 1F EE 58 00 50 00 B4 03 2A 01 FB E1 16 07 1F EE 5A 00 50 00 B4 03 2A 01 FB E1 18 07 1F EE 5C 00 50 00 B4 03 2A 01 FB E1 1A 07 1F EE 5E 00 50 00 B4 03 2A 01 FB E1 1C 07 1F EE 60 00 50 00 B4 03 2A 01 FB E1 1E 07 1F EE 62 00 50 00 B4 03 2A 01 FB E1 20 07 1F EE 64 00 50 00 B4 03 2A 01 FB E1 22 07 1F EE 66 00 50 00 B4 03 2A 01 FB E1 24 07 1F EE 68 00 50 00 B4 03 2A 01 FB E1 26 07 1F EE 6A 00 50 00 B4 03 2A 01 FB E1 28 07 1F EE 6C 00 50 00 B4 03 2A 01 FB E1 2A 07 1F EE 6E 00 50 00 B4 03 2A 01 FB E1 2C 07 1F EE 70 00 50 00 B4 03 2A 01 FB E1 2E 07 1F EE 72 00 50 00 B4 03 2A 01 FB E1 30 07 1F EE 74 00 50 00 B4 03 2A 01 FB E1 32 07 1F EE 76 00 50 00 B4 03 2A 01 FB E1 34 07 1F EE 78 00 50 00 B4 03 2A 01 FB E1 36 07 1F EE 7A 00 50 00 B4 03 2A 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 00 22 0A 69 FB"; //急刹车事件场景

	wstring strHurryDecelerateScene2 = L"";//急减速事件场景2
	wstring strHurryAccelerationScene2 = L"";//急加速事件场景2
	wstring strHurrySwervesScene2 = L""; //急转弯事件场景2
	wstring strImpactEventScene2 = L""; //碰撞事件场景2
	wstring strHurryBrakesEventScene2 = L""; //急刹车事件场景2

	wstring strXingChengStart = L"FA FA 7C 97 00 09 8C 73 B9 6F 00 25 09 21 31 11 07 0A 01 57 C9 F6 06 CA 8D F0 00 00 3B 00 00 01 22 06 00 00 00 91 00 01 00 00 01 FF 78 00 00 23 6C 5C FB"; //行程开始
	wstring strXingChengEnd = L"FA FA 7C 97 00 09 8C 73 B9 6F 00 25 09 21 31 11 07 0A 01 57 C9 F6 06 CA 8D F0 00 00 3B 00 00 01 22 06 00 00 00 91 00 02 00 00 01 FF 78 00 00 23 6C 5C FB"; //行程结束

	typedef unsigned char BoxId[12];
	typedef unsigned char Imsi[16]; //十五字节
	typedef unsigned char Iccid[21];
	typedef unsigned char Vin[18];
	typedef unsigned char SimNumber[21];
	typedef unsigned char IdVerifySeed[4];
	UBox();

	byte curLocationX = 0x00;//当前经度第三个字节
	byte curLocationY = 0x00;//当前纬度第三个字节
	struct Frame
	{
		vector<unsigned char> mData;
		DWORD mTickCount;
	};
	typedef vector<Frame> FrameBuffer;
	typedef vector<Frame>::const_iterator FrameBufferConstIt;
	typedef vector<Frame>::iterator FrameBufferIt;

	enum FrameType {
		GpsInfo,
		CanStaticData,
		CarFault,
		VersionInfo,
		TerminalWarning,
		TerminalHeartbeat,
		ActionData,
		SimInfo,
		GpsLbsInfo,
		LbsInfo,
		CarInfo,
		HurryDecelerate,//急减速
		HurryAcceleration, //急加速
		HurrySwerves,//急转弯
		OverSpeed,//超速
		HurryBrakes,//急刹车

		Log,
		AlarmEvent,
		ExtendEvent,
		BigGpsInfo,
		BigCanStaticData,
		HurryDecelerateScene,
		HurryAccelerationScene,
		HurrySwervesScene,
		ImpactEventScene,
		HurryBrakesEventScene,
		HurryDecelerateScene2,
		HurryAccelerationScene2,
		HurrySwervesScene2,
		ImpactEventScene2,
		HurryBrakesEventScene2,

		XingChengStart,
		XingChengEnd,

		FrameInfo //发送Edit帧
	};

	BoxId mBoxId;
	Imsi mImsi;
	Iccid mIccid;
	SimNumber mSimNumber;
	Vin mVin;
	wstring mServerName;
	int mServerPort;

	FrameBuffer mFrameBuffer;
	Frame mCurrentFrame;

	SOCKET mSock;
	volatile bool mReceiveFuncQuit;
	HANDLE mReceiveFuncHandle;
	AutoMutex mReceiveMutex;
	vector<unsigned char> mReceiveBuffer;
	IdVerifySeed mIdVerifySeed;
	int packetSeq;

	//心跳函数
	void onTimer();
	void onReceive(const unsigned char * buf, size_t count);
	void heartbeat();
	bool boxIdValid() const { return *((unsigned int*)mBoxId) != 0; }
	bool inTravel() const { return false; }
	void takeReceiveBuffer(vector<unsigned char> & buf);
	void disconnect();
	void SendFarmeInfo(FrameType ft, wstring frame = L"");
	void CloseSocket();
	static unsigned __stdcall receiveFunc(void *);
	//获取包序列
	int getPacketSeq()
	{
		if (packetSeq > 114)
			packetSeq = 0;
		else
			packetSeq++;
		return packetSeq;
	}
	int random(int start, int end)
	{
		return (rand() % (end - start + 1)) + start;
	}
	void Trim(wstring &s);
};

